// pages/order/order.js
var app = getApp();
var url = app.globalData.url;
var userId;
var openId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hide: 'hide',
    shippingname:'请选择',
    shippingtype:"none",
    URL: url,
    goodslist:[],
    shippingList:[],
    total_price:[],
    address:{},
    address_id:'',
    shipping_code:'',
    user_note:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    userId = wx.getStorageSync('user_id');
    openId = wx.getStorageSync('open_id');
    // console.log(options)
    var that = this;
    var goodsObj = JSON.parse(options.data);
    that.setData({
      goodslist: goodsObj.cartList,
      shippingList: goodsObj.shippingList,
      total_price: goodsObj.total_price,
    });
    wx.request({
      url: url + "/api/cart/ajaxAddress",
      data: {
        user_id: userId,
      },
      success: function (res) {
        console.log(res);
        for (var i in res.data.address_list) {
          if (res.data.address_list[i]['is_default'] == 1) {
            for (var r in res.data.regionList) {
              // console.log(r);
              if (res.data.address_list[i]['province'] == r) {
                var province_cn = res.data.regionList[r];
              }
              if (res.data.address_list[i]['city'] == r) {
                var city_cn = res.data.regionList[r];
              }
              if (res.data.address_list[i]['district'] == r) {
                var district_cn = res.data.regionList[r];
              }
            }
            // console.log(address);
            that.setData({
              address: {
                consignee: res.data.address_list[i]['consignee'],
                mobile: res.data.address_list[i]['mobile'],
                province_cn: province_cn,
                city_cn: city_cn,
                district_cn: district_cn,
                address: res.data.address_list[i]['address'],
              },
              address_id: res.data.address_list[i]['address_id'],
            });
          }
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: '创建订单'
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  selectshipping:function(){//选择物流
    // console.log(1);
    this.setData({
      shippingtype:"block",
    });
  },
  radioChange:function(r){
    var shippingStr = r.detail.value;
    var shippingArr = new Array();
    shippingArr = shippingStr.split(",");
    console.log(shippingArr);
    this.setData({
      shippingtype: "none",
      shippingname: shippingArr[1],
      shipping_code: shippingArr[0],
    });
  },
  animationin:function(){
    var that = this;
    // console.log(1);
    wx.request({
      url: url + '/api/user/address_list',
      method: 'POST',
      data: {
        user_id: userId,
      },
      success: function (res) {
        console.log(res.data.lists);

        that.setData({
          hide:'',
          sitelist: res.data.lists
        })
      }
    })
  },
  selectaddress:function(u){
    console.log(u.currentTarget.dataset.index);
    var index = u.currentTarget.dataset.index
    // console.log(address);
    this.setData({
      hide:'hide',
      address: {
        consignee: index.consignee,
        mobile: index.mobile,
        province_cn: index.province_cn,
        city_cn: index.city_cn,
        district_cn: index.district_cn,
        address: index.address,
        address_id: index.address_id,
      }
    });
  },
  submit_order:function(sub){
    // console.log(sub.currentTarget.dataset);
    var data = sub.currentTarget.dataset;
    // console.log(data.user_note);
    wx.request({
      url: url + "/api/cart/shop_cart_third",
      method:"POST",
      data:{
        user_id:userId,
        address_id:data.adderess_id,
        shipping_code:data.shipping_code,
        invoice_title:'',
        user_note:data.user_note,
        act:'submit_order',
      },
      success:function(res){
        console.log(res);
        if (res.data.status == -4){
          wx.showToast({
            title: res.data.msg,
            image:'',
            icon: 'success',
            duration: 2000
          })
        }else{
          console.log(res);
          wx.request({
            url: url + '/api/payment/getxcxPay',
            data: {
              openid: openId,
              user_id:userId,
              pay_radio: 'pay_code=weixinsmall',
              order_id: res.data.result,
            },
            success: function (res) {
              // console.log(res.data.timeStamp);
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': 'prepay_id=' + res.data.package,
                'signType': 'MD5',
                'paySign': res.data.paySign,
                'success': function (res) {
                  console.log('1'+res);
                  // wx.navigateTo({
                  //   url: '/pages/user/user',
                  // })
                },
                'fail': function (res) {
                  // console.log('2'+res);
                  wx.redirectTo({
                    url: '/pages/orderlist/orderlist',
                  })
                }
              })
            }
          });
        }
      }
    })
  },
  userNote:function(r){
    console.log(r);
    this.setData({
      user_note: r.detail.value,
    });
  },
  toadd:function(){
    wx.redirectTo({
      url: '/pages/sitelist/sitelist',
    })
  }
})